package com.fengye.middleware.rabbitmq.boot.dead.letter;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 死信队列 rabbitMQ 配置
 * @author sky
 * @date 2021年06月10日 10:15 上午
 */
@Configuration
public class DeadLetterRabbitConfig {

    @Value("${mq.dead.queue.name}")
    private String deadLetterQueueName;

    @Value("${mq.dead.exchange.name}")
    private String deadLetterExchangeName;

    @Value("${mq.dead.routing.key.name}")
    private String deadRoutingKeyName;


    @Value("${mq.producer.basic.exchange.name}")
    private String basicProducerExchangeName;

    @Value("${mq.producer.basic.routing.key.name}")
    private String basicProducerRoutingKeyName;


    /**
     * 死信队列
     * @return org.springframework.amqp.core.Queue
     */
    @Bean("basicDeadQueue")
    public Queue basicDeadQueue() {
        Map<String, Object> args = new HashMap<>();

        // 死信交换机名称
        args.put("x-dead-letter-exchange", deadLetterExchangeName);

        // 死信路由
        args.put("x-dead-letter-routing-key", deadRoutingKeyName);

        // 设置 TTL，单位是 ms，10000ms = 10s
        args.put("x-message-ttl", 10000);

        return new Queue(deadLetterQueueName, true, false, false, args);
    }

    /**
     * 创建交换机 - 面向生产者
     * @return org.springframework.amqp.core.TopicExchange
     */
    @Bean("basicProducerExchange")
    public TopicExchange basicProducerExchange() {
        return new TopicExchange(basicProducerExchangeName, true, false);
    }

    /**
     * 创建"基本消息模型"的基本绑定 - 基本交换机+基本路由 （面向消费者）
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding basicProducerBinding(@Qualifier("basicDeadQueue") Queue basicDeadQueue,@Qualifier("basicProducerExchange") TopicExchange basicProducerExchange) {
        return BindingBuilder.bind(basicDeadQueue).to(basicProducerExchange).with(basicProducerRoutingKeyName);
    }

    @Value("${mq.consumer.real.queue.name}")
    private String realConsumerQueueName;


    /**
     * 创建真正的队列，面向消费者
     * @return org.springframework.amqp.core.Queue
     */
    @Bean
    public Queue realConsumerQueue() {
        return new Queue(realConsumerQueueName,true);
    }


    /**
     * 创建真正的交换机,面向消费者
     * @return org.springframework.amqp.core.TopicExchange
     */
    @Bean
    public TopicExchange basicDeadExchange() {
        return new TopicExchange(deadLetterExchangeName,true,false);
    }


    /**
     * 创建死信路由及其绑定
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding basicDeadBinding() {
        return BindingBuilder.bind(realConsumerQueue()).to(basicDeadExchange()).with(deadRoutingKeyName);
    }


}
